package com.sky.service.impl;


import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.exception.AddressBookBusinessException;
import com.sky.exception.ParamMissException;
import com.sky.mapper.UserAddressBookMapper;
import com.sky.service.UserAddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class UserAddressBookServiceImpl implements UserAddressBookService {
    @Autowired
    private UserAddressBookMapper userAddressBookMapper;
    @Override
    public List<AddressBook> selectUserList() {
        Long currentId = BaseContext.getCurrentId();
        AddressBook addressBook = AddressBook.builder().userId(currentId).build();
        return userAddressBookMapper.selectUserList(addressBook);
    }

    @Override
    public void insertAddress(AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBook.setCreateTime(LocalDateTime.now());
        addressBook.setIsDefault(0);
        userAddressBookMapper.insertAddress(addressBook);
    }

    @Override
    public void defaultAddressBook(AddressBook addressBook) {
        //1、将当前用户的所有地址修改为非默认地址 update address_book set is_default = ? where user_id = ?
        addressBook.setIsDefault(0);
        addressBook.setUserId(BaseContext.getCurrentId());
        userAddressBookMapper.defaultAddressBook(addressBook);

        //2、将当前地址改为默认地址 update address_book set is_default = ? where id = ?
        addressBook.setIsDefault(1);
        userAddressBookMapper.update(addressBook);

    }

    @Override
    public AddressBook findByid(Long id) {
        AddressBook condition = AddressBook.builder().id(id).build();
        List<AddressBook> addressBooks = userAddressBookMapper.selectUserList(condition);
        if (addressBooks.size()==0||addressBooks==null){
            throw new AddressBookBusinessException(MessageConstant.ADDRESS_BOOK_NOT_EXISTS);
        }
        return addressBooks.get(0);
    }

    @Override
    public void update(AddressBook addressBook) {
        Long id = addressBook.getId();
        if (id==null){
            throw new ParamMissException(MessageConstant.PARAMS_MISSED);
        }
        /*Long currentId = BaseContext.getCurrentId();
        Long userId = addressBook.getUserId();
        if (currentId.longValue()==userId.longValue()){
            throw new AddressBookBusinessException("警告:请更改自己的地址");
        }*/
        //更新
        userAddressBookMapper.update(addressBook);
    }

    @Override
    public void delete(String id) {
        userAddressBookMapper.delete(id);

    }

    @Override
    public AddressBook selectDefault() {
        Long currentId = BaseContext.getCurrentId();
        AddressBook condition = AddressBook.builder().userId(currentId).build();
        List<AddressBook> addressBooks = userAddressBookMapper.selectUserList(condition);
        return addressBooks.get(0);
    }
}
